import { reqLogin } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'
const state = {
  token: getToken() || ''
}
// mutations是唯一修改state的途径
const mutations = {
  setToken(state, newToken) {
    state.token = newToken
    setToken(newToken)
  }
}
const actions = {
  async login(context, loginForm) {
    /**
     * 1. 发送登录请求
     * 2. 拿到token 存入vuex
     */
    // const { data: { data: token }} = await reqLogin(loginForm)
    const res = await reqLogin(loginForm)
    // res.data.data
    // console.log(res.data.data)
    const token = res.data
    context.commit('setToken', token)
  }
}
export default {
  state,
  mutations,
  actions,
  namespaced: true

}
